package com.cat.model.web.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import com.alonew.core.commons.util.CommonUtil;

public class HttpUtils {

	public static final long GET_MAX_SIZE = 2097152L;

	public static String get(String url, String encode) {
//		System.out.println(url);
		HttpClient client = null;
		GetMethod gm = null;
		String result = null;
		try {
			client = new HttpClient();
//			client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encode);
			gm = new GetMethod(url);
//			gm.setRequestHeader("", encode);
//			gm.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
			client.executeMethod(gm);
			result = gm.getResponseBodyAsString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (gm != null) {
				try {
					gm.releaseConnection();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static String post(String url, String encode) {
		HttpClient client = null;
		PostMethod post = null;
		String result = null;
		try {
			client = new HttpClient();
//			client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encode);
			post = new PostMethod(url);
//			post.setRequestHeader("X-Forwarded-For", "11.10.10.151");
//			NameValuePair[] values = {new NameValuePair("msg", "bb2"),new NameValuePair("aa1", "b2b")};
//			post.setRequestBody(values);
//			post.setRequestBody("{\"msg\":\"ade\"}");
			client.executeMethod(post);
			result = post.getResponseBodyAsString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (post != null) {
				try {
					post.releaseConnection();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	
	public static String postFromIp(String url, String ip) {
		HttpClient client = null;
		PostMethod post = null;
		String result = null;
		try {
			client = new HttpClient();
			post = new PostMethod(url);
			post.setRequestHeader("X-Real-IP", ip);
			post.setRequestHeader("X-Forwarded-For", ip);
			post.setRequestHeader("Proxy-Client-IP", ip);
			post.setRequestHeader("WL-Proxy-Client-IP", ip);
			client.executeMethod(post);
			result = post.getResponseBodyAsString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (post != null) {
				try {
					post.releaseConnection();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static int status(String url, String encode) {
		HttpClient client = new HttpClient();
		HeadMethod hm = new HeadMethod(url);
//		client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encode);
		try {
			client.executeMethod(hm);
			return hm.getStatusCode();
		} catch (Exception e) {
			e.printStackTrace();
			return 500;
		} finally {
			hm.releaseConnection();
		}
	}
	
	public static String doPost(String url, List<NameValuePair> nvps){
		HttpPost post = new HttpPost(url);
		CloseableHttpClient client = HttpClientBuilder.create().build();

		post.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));

		try {
			HttpResponse response = client.execute(post);
			StatusLine statusLine = response.getStatusLine();
			if (statusLine.getStatusCode() == HttpStatus.SC_NOT_ACCEPTABLE) {
				return null;
			}else if (statusLine.getStatusCode() >= 300) {
				throw new RuntimeException(statusLine.getStatusCode() + "|" + statusLine.getReasonPhrase());
			} else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					String content = EntityUtils.toString(entity);
					return content;
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}finally{
			if(client != null){
				try {
					client.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	
	public static String doPost(String url, String xml){
		HttpPost post = new HttpPost(url);
		CloseableHttpClient client = HttpClientBuilder.create().build();
		StringEntity entity1 = null;
		try {
			entity1 = new StringEntity(xml);
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		post.setEntity(entity1);
		post.setHeader("Content-Type", "text/xml;charset=UTF-8");

		try {
			HttpResponse response = client.execute(post);
			StatusLine statusLine = response.getStatusLine();
			if (statusLine.getStatusCode() == HttpStatus.SC_NOT_ACCEPTABLE) {
				return null;
			}else if (statusLine.getStatusCode() >= 300) {
				throw new RuntimeException(statusLine.getStatusCode() + "|" + statusLine.getReasonPhrase());
			} else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					String content = EntityUtils.toString(entity, "UTF-8");
					return content;
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}finally{
			if(client != null){
				try {
					client.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	
	public static void main(String[] args){
//		String result = postFromIp("http://localhost:8081/lenovo-bless/test/t/iptest", "127.0.0.1");
//		System.out.println(result);
		int count = 0;
		int num = 0;
		while (true) {
			count++;
			int ip1 = CommonUtil.RANDOM.nextInt(255);
			int ip2 = CommonUtil.RANDOM.nextInt(255);
			int ip3 = CommonUtil.RANDOM.nextInt(255);
			int ip4 = CommonUtil.RANDOM.nextInt(255);
			String ip = ip1 + "." + ip2 + "." + ip3 + "." + ip4;
			String result = postFromIp("http://ud.fj.qq.com/api/activity/4916/vote/image/25527/jsonp", ip);
			if(result.contains("vote_weight")){
				num++;
			}
			System.out.println(ip + "|" + count + "|" + num + "\t" + result);
			try {
				TimeUnit.SECONDS.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
